import matplotlib.pyplot as plt
from matplotlib.patches import Polygon
from matplotlib.ticker import NullLocator
from typing import TYPE_CHECKING, Sequence

from puzzle.mathF import get_center
from container import Tiles, OPTIONS, v_get_face_color_by_td, LABEL_DAY

if TYPE_CHECKING:
    from matplotlib.axes import Axes
    from puzzle.board import Board
    from puzzle.mathF import Vec2
    from container import color_t


def prepare_ax(ax: "Axes"):
    ax.set_xlim(-OPTIONS.V_WIDTH / 2, OPTIONS.V_WIDTH / 2)
    ax.set_ylim(-OPTIONS.V_HEIGHT / 2, OPTIONS.V_HEIGHT / 2)
    ax.set_aspect('equal')
    ax.axis('off') # Hide axis
    

# def plot_board(ax: "Axes", board: "Board") -> None:
#     lb = ["Jan", "Feb", "Mar", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec", "10", "11", "22", "21", "31", "30", "Sun", "Sat", "Fri", "Thu", "Wed", "Tue", "Mon", "24", "23", "13", "12", "1", "2", "Apr", "May"]
#     tx = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "a", "b", "d", "e"]
    
#     cl = [1,2,1,2,3,0,3,4,3,4,5,4,5,0,5,6,5,6,0,6,7,6,7,0,7,2,7,2,0,2]
    
#     for td in board.tiles_flatten:
#         if td.tile == Tiles.blank:
#             continue
        
#         center = get_center(td.vertices)
        
#         if td.tile.label in lb:
#             idx = lb.index(td.tile.label)
            
#             if cl[idx] == 0:
#                 facecolor = (1, 1, 1)
#             else:
#                 facecolor = OPTIONS.V_COMP_COLOR[cl[idx] + 2]
#             draw_polygon(ax, td.vertices, facecolor)
            
#             ax.text(center.x, center.y, tx[idx], ha="center", va="center", fontsize = 13)
#         else:
#             draw_polygon(ax, td.vertices, (1, 1, 1))

# cntup = 0
# cntdown = 0

# def plot_board(ax: "Axes", board: "Board") -> None:
#     for td in board.tiles_flatten:
#         if td.tile == Tiles.blank:
#             continue
        
#         global cntup, cntdown
#         if td.is_facing_up:
#             cntup += 1
#             facecolor = (1, 1, 1)
#         else:
#             cntdown += 1
#             facecolor = OPTIONS.V_FACE_COLOR_MONTH
#         print(cntup, cntdown)
        
#         draw_polygon(ax, td.vertices, facecolor)
            
#         center = get_center(td.vertices)
#         ax.text(center.x, center.y, td.tile.label, ha="center", va="center")

# def plot_board(ax: "Axes", board: "Board") -> None:
#     for td in board.tiles_flatten:
#         if td.tile == Tiles.blank:
#             continue
        
#         if td.tile.label in LABEL_DAY and td.tile.label != "31":
#             facecolor = v_get_face_color_by_td(td)
#             draw_polygon(ax, td.vertices, facecolor)
            
#             center = get_center(td.vertices)
#             ax.text(center.x, center.y, td.tile.label, ha="center", va="center")
#             continue
        
#         l = ["Jan", "Mon", "Feb", "Thu", "Jun", "Sat", "Nov", "31", "Dec", "Sun", "Oct", "Fri", "May", "Wed", "Apr", "Tue", "Mar"]
#         if td.tile.label not in l:
#             match td.tile.label:
#                 case "Jul": lb = "-"
#                 case "Sep": lb = "+"
#                 case "Aug": lb = "*"
            
#             draw_polygon(ax, td.vertices, None)
#             center = get_center(td.vertices)
#             ax.text(center.x, center.y, lb, ha="center", va="center", fontsize = 12)
            
#             continue
        
#         idx = l.index(td.tile.label)
#         c = chr(ord("z") - idx)
        
#         match c:
#             case "x" | "v" | "t": facecolor = OPTIONS.V_COMP_COLOR[1]
#             case "w" | "y": facecolor = OPTIONS.V_COMP_COLOR[2]
#             case "m" | "o": facecolor = OPTIONS.V_COMP_COLOR[3]
#             case "u" | "s" | "q": facecolor = OPTIONS.V_COMP_COLOR[4]
#             case "j" | "l" | "n" | "p" | "r": facecolor = OPTIONS.V_COMP_COLOR[5]
#             case _: facecolor = None
        
#         draw_polygon(ax, td.vertices, facecolor)
#         center = get_center(td.vertices)
#         ax.text(center.x, center.y, c, ha="center", va="center", fontsize = 12)

def plot_board(ax: "Axes", board: "Board") -> None:
    for td in board.tiles_flatten:
        if td.tile == Tiles.blank:
            continue
        
        if td.tile.label in "12 13 14 15 16 Dec 17 18 19 20".split(" "):
            facecolor = v_get_face_color_by_td(td)
            draw_polygon(ax, td.vertices, facecolor)
            
            center = get_center(td.vertices)
            ax.text(center.x, center.y, td.tile.label, ha="center", va="center")
            
        elif td.tile.label in "Nov 11 Sun".split(" "):
            draw_polygon(ax, td.vertices, None)
            center = get_center(td.vertices)
            ax.text(center.x, center.y, td.tile.label, ha="center", va="center")

def draw_polygon(
    ax: "Axes",
    vertices: Sequence["Vec2"],
    facecolor: "color_t | None",
    edgecolor: "color_t" = OPTIONS.V_EDGE_COLOR,
    linewidth: float | None = None,
) -> None:
    if facecolor is None:
        ax.add_patch(Polygon(
            vertices,
            edgecolor = edgecolor,
            fill = False,
            linewidth = linewidth,
        ))
    else:
        ax.add_patch(Polygon(
            vertices,
            edgecolor = edgecolor,
            facecolor = facecolor,
            fill = True,
            linewidth = linewidth,
        ))
        

def show() -> None:
    plt.show()